<?php
set_time_limit(0); //防止执行超时
ob_end_clean();   //清空并关闭输出缓冲区

if(!isset($_GET["id"])){
    echo "pls input id!";
    return;
}

include("base_function.php");
include("base/db.php");


function doit()
{
    open_db();
    $parent_catalogs = query_db("select * from pm_catalogs","id");

    $html = http_response($parent_catalogs[$_GET["id"]]["url"]);
    $html = htmlspecialchars_decode($html,ENT_QUOTES);
    
    $reg = '|<div class="cat-link" id="cat-dash"><a  href="(.*?)" _sp=".*?" >(.*?)</a><span class="cnt">.*?</span></div>|';
    preg_match_all ($reg,  
        $html,
        $out, PREG_PATTERN_ORDER);
    if(count($out) != 0){
        for($i = 0;$i < count($out[2]);++$i){
            $exist = query_db("select * from pm_sub_catalog where url='".$out[1][$i]."'","id");
            print_r($exist);
            if(!$exist){
                //echo "insert into pm_sub_catalog (catalog,parent_catalog_id,url) values ('".$out[2][$i]."','".$_GET["id"]."','".$out[1][$i]."')\r\n";
             exec_db("insert into pm_sub_catalog (catalog,parent_catalog_id,url) values ('".$out[2][$i]."','".$_GET["id"]."','".$out[1][$i]."')");
            }
        }
    }
    //print_r($sub_catalogs);
    close_db();
}
doit();

echo "ok";

?>
